﻿/**
 * 程序名称：任务执行框架JJYChengTaskWinForm-
 * 作者：jjycheng
 * 博客：https://blog.csdn.net/cplvfx
 * gitee:https://gitee.com/jjycheng
 **/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using JJYChengTaskWinForm.Enums.EnumCommon.Models;

namespace JJYChengTaskWinForm.Enums.EnumCommon
{
    public static class EnumHelper
    {
        public static List<EnumInfo> GetEnumInfo<TEnum>() where TEnum : Enum
        {
            var result = new List<EnumInfo>();

            foreach (var field in typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                if (field.FieldType != typeof(TEnum)) continue;

                var name = field.Name;
                var value = (int)field.GetValue(null);

                var descriptionAttr = field.GetCustomAttributes<DescriptionAttribute>().FirstOrDefault();
                var colorAttr = field.GetCustomAttributes<ColorAttribute>().FirstOrDefault();

                var description = descriptionAttr?.Description ?? "无描述";
                var color = colorAttr?.ColorCode ?? "无颜色";

                result.Add(new EnumInfo
                {
                    Key = name,
                    Value = value,
                    Description = description,
                    ColorCode = color
                });
            }

            return result;
        }
    }
}
